package com.humi.partner.manager.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.humi.cloud.common.model.ResponseCode;
import com.humi.cloud.common.model.Result;
import com.humi.cloud.common.utils.BeanUtil;
import com.humi.cloud.mybatis.support.model.Page;
import com.humi.partner.common.entity.PartnerType;
import com.humi.partner.common.service.PartnerTypeService;
import com.humi.partner.manager.dao.ManagerPartnerTypeMapper;
import com.humi.partner.manager.model.partnerType.PartnerTypeAddQueryRequest;
import com.humi.partner.manager.model.partnerType.PartnerTypeAddQueryResponse;
import com.humi.partner.manager.model.partnerType.PartnerTypeListQueryRequest;
import com.humi.partner.manager.model.partnerType.PartnerTypeListQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * @Auther: lishijie
 * @Date: 2019/10/11 14:12
 * @Description:Codes are far away from bugs with the animal protecting
 */
@Service
public class ManagerPartnerTypeService extends ServiceImpl<ManagerPartnerTypeMapper,PartnerType>{

    @Autowired
    ManagerPartnerTypeMapper managerPartnerTypeMapper;

    @Autowired
    PartnerTypeService partnerTypeService;
    private String partner;

    /**
     * 分页查询伙伴类型信息
     * @param request
     * @return
     */
    public Result getPertnerType(PartnerTypeListQueryRequest request){
        Result result = new Result();
        Page< PartnerTypeListQueryResponse,PartnerTypeListQueryRequest> page = new Page<>(request.getCurrent(),request.getSize(),request);
        this.managerPartnerTypeMapper.getPartnerTypePages(page);
        result.setData(page);
        return result;
    }

    /**
     * 判断是否启用
     * @param id
     * @param  flag  前端传来的要修改到的目标值
     * @return
     */
    public Result getuseFlag(String id,int flag){
        Result result = new Result();
        //PartnerType partnerType = this.getById(id);
        PartnerType partnerType = this.getOne(new LambdaQueryWrapper<PartnerType>().eq(PartnerType::getId,id));
        //判断数据是否存在
        if(null == partnerType){
            Result.fail("该条数据不存在");
        }
        int useFlagOld = partnerType.getUseFlag();//原状态
        // 判断flag  是否为0 或者1  不能为其他数字
        if(flag != 0 && flag != 1){
            return Result.fail("当前启用类型只能是（0 或者1）  不能为其他数字");
        }
        if (flag == useFlagOld){  //如果要修改的状态和现在的状态一样  则不修改
            return Result.fail("当前状态与要修改的状态一致");
        }
        partnerType.setUseFlag(flag);
        this.updateById(partnerType);
        result.setMessage("启用修改成功");
        return result;
    }

    /**
     * 新增和修改
     * @param request
     * @return
     */
    @Transactional
    public Result getPertnerTypeAdd(PartnerTypeAddQueryRequest request){
        String id = request.getId();
        PartnerType partnerType = null;
        if (StringUtils.isBlank(id)){//无ID 说明为新增
            partnerType = new PartnerType();
        }else { //有ID 说明为修改
            partnerType = this.getOne(new LambdaQueryWrapper<PartnerType>().eq(PartnerType::getId, request.getId()));
        }
        //获取伙伴类型实体类
        //PartnerType partnerType = this.getById(partnerType);方法二
        if(partnerType != null) {
            BeanUtil.copyProperties(request,partnerType);
        }
        //执行添加或修改操作
        boolean saveOrUpdated =false;
        saveOrUpdated = partnerTypeService.saveOrUpdate(partnerType);

        if(!saveOrUpdated){
            return Result.fail("修改伙伴类型失败");
        }
        return Result.ok();
    }

    /**
     * <pre>
     *	获取伙伴类型管理详情
     * </pre>
     * @param id
     * @return
     */
    public Result getPartnerTypeOrderDetail(String id) {
        PartnerTypeAddQueryResponse data = this.baseMapper.getPartnerTypeOrderDetail(id);
        if(data == null) {
            return Result.argumentFail("伙伴信息不存在");
        }
        return Result.ok(data);
    }




    /**
     * 删除伙伴类型
     * @param id
     * @return
     */
    public Result delPartnerTypeById(String id){
        Result result = new Result();
        PartnerType partnerType = this.getById(id);
        Integer useCount = partnerType.getUseCount();
        if(useCount != 0){
            result.build(ResponseCode.FAILED,"该类型下已有伙伴加入，不能直接删除，只能关闭启用");
        }
        if (StringUtils.isBlank(id)) {
            result.build(ResponseCode.FAILED, "参数不能为空");
            return result;
        }
        this.removeById(partnerType);
        result.setMessage("删除成功");
        return result;
    }



}
